package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommondQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    ITravelCommendService travelCommendService;

    /**
     * 查询推荐游记列表
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelCommondQueryObject qo) {
        PageInfo<TravelCommend> pageInfo = travelCommendService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/travelCommend/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(TravelCommend travelCommend,MultipartFile file) {
        JsonResult result = new JsonResult();
        //如果有重新上传背景图片就把之前的背景图片覆盖
        if (file != null && file.getSize() > 0) {
            String coverUrl = UploadUtil.uploadQiniu(file);
            travelCommend.setCoverUrl(coverUrl);
        }
        travelCommendService.saveOrUpdate(travelCommend);

        return result;
    }

}
